#ifndef EXTRACTLINES_H
#define EXTRACTLINES_H

#include <vector>
#include <opencv2/core/core.hpp>

#include "DataStructures.h"

/**
  * Extracts lines from a contour by finding corners and using a divide and conquer strategy
  */
class ExtractLines
{

public:
    ExtractLines( int minLinePoints , double lineFitTol , bool findSmallSegments );
    ~ExtractLines();

    void extract( const std::vector<cv::Point> &contour ,
                  const std::vector<ContourCorner> &corners );

    const std::vector<LineSegmentF>& getFound() const;

    void configure( double lineFitTol );

private:

    void findLines( std::vector<cv::Point>& list );

    void findLines( const std::vector<cv::Point>& origList ,
                    int begin , int end  ,
                    std::vector<cv::Point>& list );

    LineSlopeF fitLine( std::vector<cv::Point>& list );

    void putIntoList( const std::vector<cv::Point> &contour ,
                      int start , int stop ,
                      std::vector<cv::Point> &list );

    void saveLine( LineSlopeF line , cv::Point a , cv::Point b );

    // how close a point needs to be a line for it to be considered part of the line
    double lineFitTol;

    // minimum number of points that can be in a line
    int minLinePoints;

    // list of all the lines it has found
    std::vector<LineSegmentF> lines;

    // find smaller lines using divide and conquer
    bool findSmallSegments;

};

#endif // EXTRACTLINES_H
